package com.empire.module.sporttery.vo;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 足彩 胜负平表
 *
 * @author qihang
 * @since 2022-10-05
 */
@Data
@Accessors(chain = true)
public class FootballMatchVO implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    /**
     * 500wan的有戏Id
     */
    private Integer matchId;

    /**
     * 编号
     */
    private String number;

    /**
     * 颜色
     */
    private String color;

    /**
     * 赛事
     */
    private String match;
    /**
     * 联赛地址
     */
    private String matchUrl;
    /**
     * 分析链接
     */
    private String analysis;

    /**
     * 让球
     */
    private String letBall;

    /**
     * 让球赔率
     */
    private List<Map<String, Object>> letOddsList;

    /**
     * 欧指赔率
     */
    private List<Map<String, Object>> notLetOddsList;

    /**
     * 主队
     */
    private String homeTeam;

    /**
     * 客队
     */
    private String visitingTeam;

    /**
     * 是否单关 0 否 1 是
     */
    private String isSingle;

    /**
     * 截止时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime deadline;

    /**
     * 开赛时间
     */
    private String openTime;

    public String getScore() {
        return StrUtil.isEmpty(score)? "VS" : score;
    }

    /**
     * 比分
     */
    private String score;

    /**
     * 主队链接
     */
    private String homeTeamUrl;

    /**
     * 客队链接
     */
    private String visitTeamUrl;
    /**
     * 胜赔
     */
    private String winPay;
    private Integer winFlag;

    public String getLetVisitWinRate() {
        return replaceShoot(letVisitWinRate);
    }

    public String getLetWinRate() {
        return replaceShoot(letWinRate);
    }

    public String getLetDrawWinRate() {
        return replaceShoot(letDrawWinRate);
    }

    public String getVisitWinRate() {
        return replaceShoot(visitWinRate);
    }

    public String getDrawWinRate() {
        return replaceShoot(drawWinRate);
    }

    public String getWinRate() {
        return replaceShoot(winRate);
    }

    private String winRate;
    /**
     * 平赔
     */
    private String drawPay;
    private Integer drawWinFlag;
    private String drawWinRate;
    /**
     * 客赔
     */
    private String visitPay;
    private Integer visitWinFlag;
    private String visitWinRate;
    /**
     * 让球胜赔
     */
    private String letWinPay;
    private Integer letWinFlag;
    private String letWinRate;

    /**
     * 让球平赔
     */
    private String letDrawPay;
    private Integer letDrawWinFlag;
    private String letDrawWinRate;

    /**
     * 让球客赔
     */
    private String letVisitPay;
    private Integer letVisitWinFlag;
    private String letVisitWinRate;

    private String minusWinRate;
    private String minusDrawWinRate;
    private String minusVisitWinRate;
    /**
     * 升降幅度标识
     */
    private String minusWinUpDownFlag;
    private String minusDrawWinUpDownFlag;
    private String minusVisitWinUpDownFlag;

    /**
     * 比分推荐
     */
    private String scoreSelect;
    /**
     * 比分模拟值
     */
    private String teamScore;

    /**
     * 选它
     */
    private Integer winSelect;
    private Integer drawWinSelect;
    private Integer visitWinSelect;
    private Integer letWinSelect;
    private Integer letDrawWinSelect;
    private Integer letVisitWinSelect;

    /**
     * 筛选排序
     */
    private Integer selectSort;

    /**
     * 就球数预测算法
     */
    private String ballNumPredict;
    /**
     * 进球数推荐
     */
    private String ballNum;
    /**
     * 进球差
     */
    private String ballDiff;

    /**
     * 半全场结果
     */
    private String halfFullCourt;

    public String getBallDiff() {
        if (StrUtil.isNotEmpty(teamScore) && teamScore.indexOf(":") > 0) {
            try {
                String splitStr = "";
                if (teamScore.indexOf(":") > 0) {
                    splitStr = ":";
                }
                if (teamScore.indexOf("VS") > 0) {
                    splitStr = "VS";
                }
                String[] balls = teamScore.split(splitStr);
                String homeBall = balls[0];
                String awayBall = balls[1].substring(0, balls[1].indexOf("("));
                if(NumberUtil.isNumber(homeBall) && NumberUtil.isNumber(awayBall)) {
                    ballDiff = String.format("%.2f", Double.parseDouble(homeBall) - Double.parseDouble(awayBall));
                }
            }catch (Exception e) {

            }
        }
        return ballDiff;
    }

    /**
     * 欧赔概率
     */
    private String ouHomeRate;
    private String ouDrawRate;
    private String ouVisitRate;
    /**
     * 欧赔算法差异，不一致则认为是反
     */
    private String ouResult;

    public String getMinusVisitWinRate() {
        if (StrUtil.isNotEmpty(visitWinRate) && StrUtil.isNotEmpty(letVisitWinRate)) {
            visitWinRate = visitWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letVisitWinRate = letVisitWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            int v1 = calculateDifference(Double.parseDouble(visitWinRate) , Double.parseDouble(letVisitWinRate));
            minusVisitWinRate = String.valueOf( v1);
        }
        return minusVisitWinRate;
    }

    public String getMinusDrawWinRate() {
        if (StrUtil.isNotEmpty(drawWinRate) && StrUtil.isNotEmpty(letDrawWinRate)) {
            drawWinRate = drawWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letDrawWinRate = letDrawWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            int v1 = calculateDifference(Double.parseDouble(drawWinRate) , Double.parseDouble(letDrawWinRate));
            minusDrawWinRate = String.valueOf( v1);
        }
        return minusDrawWinRate;
    }

    public String getMinusWinRate() {
        if (StrUtil.isNotEmpty(winRate) && StrUtil.isNotEmpty(letWinRate)) {
            winRate = winRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letWinRate = letWinRate.replace("(\uD83C\uDC04\uFE0F)", "");

            int v1 = calculateDifference(Double.parseDouble(winRate) , Double.parseDouble(letWinRate));
            minusWinRate = String.valueOf( v1);
        }
        return minusWinRate;
    }

    public static int calculateDifference(double num1, double num2) {

        // 将两个数字转换为字符串，去掉小数点部分
        String str2 = String.format("%.6f", num2);
        String str1 = String.format("%.6f", num1);

        //如果两个数字都大于1，则直接相减
        if (num1 >=1 && num2 >= 1) {
            String dec1 = str1.substring(0,  str1.indexOf('.'));
            String dec2 = str2.substring(0, str2.indexOf('.'));

            return Integer.parseInt(dec2) - Integer.parseInt(dec1);
        }

        // 获取小数部分
        String dec1 = str1.substring(str1.indexOf('.') + 1);
        String dec2 = str2.substring(str2.indexOf('.') + 1);

        // 找到两个数字小数部分的第一位有效数字的位置
        int firstIndex1 = findFirstNonZero(dec1);
        int firstIndex2 = findFirstNonZero(dec2);

        int maxIndex = Math.max(firstIndex1, firstIndex2);

        //应该乘以10的倍数
        int big = getMappedValue(maxIndex);

        String dec11 = str1.substring(0, str1.indexOf('.') + 1) + dec1.substring(0, Math.min(dec1.length(), (maxIndex + 1)));
        String dec22 = str2.substring(0, str2.indexOf('.') + 1) + dec2.substring(0,Math.min(dec2.length(), (maxIndex + 1)));

        // 计算相减结果
        double result = Double.parseDouble(dec22) * big - Double.parseDouble(dec11) * big;

        // 根据规则四舍五入后取整数部分
        return (int) Math.round(result);
    }

    // 找到小数部分中的第一位有效数字的位置
    private static int findFirstNonZero(String decimal) {
        for (int i = 0; i < decimal.length(); i++) {
            if (decimal.charAt(i) != '0') {
                return i;
            }
        }
        return decimal.length();  // 如果没有有效数字，返回末尾
    }

    // 根据需要将数字放大倍数，调整小数点后的位数
    private static double scaleNumber(double num, int scale) {
        if (scale <= 0) return num;  // 不需要缩放
        return num * Math.pow(10, scale);  // 放大数值
    }

    // 使用 Math.pow 进行实现
    public static int getMappedValue(int n) {
        // Math.pow(10, n) 会返回 double 类型，需要转换为 int 类型
        return (int) Math.pow(10, n + 1);
    }

    private static String replaceShoot(String rate) {
        if (StrUtil.isEmpty(rate)) {
            return "";
        }
        return rate.replace("(\uD83C\uDC04\uFE0F)", "");
    }

    /**
     * 主队半场算法比分
     */
    private String homeAlgScoreBan;
    /**
     * 客队半场算法比分
     */
    private String awayAlgScoreBan;

    /**
     * 主队半场推荐比分
     */
    private String homePreScoreBan;

    /**
     * 客队半场推荐比分
     */
    private String awayPreScoreBan;

    /**
     * 主队全场算法比分
     */
    private String homeAlgScoreQuan;
    /**
     * 客队全场算法比分
     */
    private String awayAlgScoreQuan;

    /**
     * 主队全场推荐比分
     */
    private String homePreScoreQuan;

    /**
     * 客队半场推荐比分
     */
    private String awayPreScoreQuan;
    /**
     * 推荐理由
     */
    private String remark;

}
